package hander

import (
	"api-client/basic/api/request"
	"api-client/pkg"
	__ "api-client/proto"
	"github.com/gin-gonic/gin"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"log"
	"net/http"
)

func SendSms(c *gin.Context) {
	var req request.SendSms
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "参数验证失败",
			"data": nil,
		})
		return
	}

	conn, err := grpc.NewClient("127.0.0.1:8803", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	client := __.NewUserClient(conn)

	client.SendSms(c, &__.SendSmsRequest{
		Mobile:      req.Mobile,
		SendSmsCode: req.SendSmsCode,
	})

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "发送短信成功",
	})
}

func Login(c *gin.Context) {
	var req request.Login
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "参数验证失败",
			"data": nil,
		})
		return
	}

	conn, err := grpc.NewClient("127.0.0.1:8803", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	client := __.NewUserClient(conn)

	sms, err := client.Login(c, &__.LoginRequest{
		Mobile:      req.Mobile,
		SendSmsCode: req.SendSmsCode,
	})
	if err != nil {
		return
	}
	token, err := pkg.NewJWT("2211a").CreateToken(pkg.CustomClaims{
		ID: uint(sms.Id),
	})
	if err != nil {
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"date": token,
	})
}

func Info(c *gin.Context) {
	var req request.Info
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "参数验证失败",
			"data": nil,
		})
		return
	}

	conn, err := grpc.NewClient("127.0.0.1:8803", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	client := __.NewUserClient(conn)

	info, err := client.Info(c, &__.InfoRequest{
		Id: int64(req.Id),
	})
	if err != nil {
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "查看成功",
		"date": info,
	})
}

func AddMusic(c *gin.Context) {
	var req request.AddMusic
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "参数验证失败",
			"data": nil,
		})
		return
	}

	conn, err := grpc.NewClient("127.0.0.1:8803", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	client := __.NewUserClient(conn)

	client.AddMusic(c, &__.AddMusicRequest{
		Music: req.Music,
	})

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "音乐选择成功",
	})
}

func Upload(c *gin.Context) {
	file, _ := c.FormFile("file")
	log.Println(file.Filename)

	dst := "./" + file.Filename
	// 上传文件至指定的完整文件路径
	c.SaveUploadedFile(file, "./files/"+file.Filename)

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "视频上传成功",
		"date": dst,
	})
}

func AddWork(c *gin.Context) {
	var req request.AddWork
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "参数验证失败",
			"data": nil,
		})
		return
	}

	conn, err := grpc.NewClient("127.0.0.1:8803", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	client := __.NewUserClient(conn)

	client.AddWork(c, &__.AddWorkRequest{
		Name:      req.Name,
		Describe:  req.Describe,
		Ipaddress: req.Ipaddress,
		Music:     req.Img,
		Video:     req.Video,
		Img:       req.Img,
	})

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "点赞成功",
	})
}

func AddCommit(c *gin.Context) {
	var req request.AddCommit
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "参数验证失败",
			"data": nil,
		})
		return
	}

	conn, err := grpc.NewClient("127.0.0.1:8803", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	client := __.NewUserClient(conn)

	client.AddCommit(c, &__.AddCommitRequest{
		UserId:  req.UserId,
		WorkId:  req.WorkId,
		Content: req.Content,
	})

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "评论成功",
	})
}
